<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	public $data;
	public $theme_path;
	public $css_path;
	public $script_path;
	public $view_path;
	
	public function __construct(){
		parent::__construct();
		$this->init();
		$this->load_css();
		$this->load_script();
	}
	
	public function init(){
		$this->theme_path = $this->config->item('theme_path');
		$this->css_path = $this->config->item('css_path');
		$this->script_path = $this->config->item('script_path');
		$this->view_path = $this->config->item('language').'/';
		$this->data['view_path'] = $this->view_path;
		$this->data['theme_path'] = $this->theme_path;
		$this->data['script_path'] = $this->script_path;
		$this->data['css_path'] = $this->css_path;
		
		$this->set_title('');
		$this->load_lang();
	}
	
	public function load_lang(){
		$controller = $this->router->fetch_class();
		$lang_file = APPPATH.'language/'.SITE_LANG.'/'.$controller.'_lang.php';
		if (is_file($lang_file)){
			$this->lang->load($controller);
		}
	}
	
	public function set_title($title){
		$this->data['page_title'] = $title.'-'.$this->config->item('site_name');
	}
	
	public function set_keyword($keyword){
		$this->data['keyword'] = $keyword;
	}
	
	public function set_description($description){
		$this->data['description'] = $description;
	}
	
	public function load_css(){
		$this->load->config('themes',true);
		$cssfiles = $this->config->item('themes');
		foreach ($cssfiles['css'] as $key=>$css){
			$this->data['css'][$key] = $this->css_path.$css;
		}
	}
	
	public function load_script(){
		$this->load->config('themes',true);
		$cssfiles = $this->config->item('themes');
		foreach ($cssfiles['js'] as $key=>$js){
			$this->data['js'][$key] = $this->script_path.$js;
		}
	}
	
	public function add_script($filename,$path='',$key=null){
		if (is_null($key)){
			$key = substr($filename, 0, stripos($filename, '.'));
		}
		$script_path = empty($path)?$this->script_path:$path;
		$this->data['js'][$key] = $script_path.$filename;
	}
	
	public function add_css($filename,$path='',$key=null){
		if (is_null($key)){
			$key = substr($filename, 0, stripos($filename, '.'));
		}
		
		$css_path = empty($path)?$this->css_path:$path;
		$this->data['css'][$key] = $css_path.$filename;
	}
	
	public function rm_css($key){
		if (isset($this->data['css'][$key])){
			unset($this->data['css'][$key]);
		}
	}
	
	public function rm_script($key){
		if (isset($this->data['js'][$key])){
			unset($this->data['js'][$key]);
		}
	}
	
	public function assign(){
		$params = func_get_args();
		if ( count($params)==2 ){
			if (is_string($params[0])){
				$this->data[$params[0]] = $params[1];
			}
			elseif(is_array($params[0])){
				foreach ($params[0] as $key=>$val){
					$this->data[$key] = $val;
				}
			}
		}
		else{
			if ( is_array($params[0]) ){
				foreach ($params[0] as $key=>$val){
					$this->data[$key] = $val;
				}
			}
		}
	}
	
	public function success($jumpurl,$msg='',$isjump=true){
		$data = array('jumpurl'=>$jumpurl,'msg'=>$msg, 'isjump'=>$isjump);
		$this->display('success',$data,SITE_LANG.'/common/');
	}
	
	public function display($file,$data=array(),$basepath=''){
		$basepath = empty($basepath)?$this->view_path.$this->router->fetch_class().'/':$basepath;
		if ( count($data) ){
			foreach ($data as $key=>$val){
				$this->data[$key] = $val;
			}
		}
		$this->load->view($basepath.$file,$this->data);
	}
	
	public function is_post(){
		if ($_SERVER['REQUEST_METHOD']=='POST'){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function is_ajax(){
		return $this->input->is_ajax_request() ? true :false;
	}
}